#include "stdafx.h"
#include <XParticle.h>
#include <OpenGL\freeglut.h>

XParticle::XParticle(int time, int duration, XSprite* sprite, XTexture* texture)
{
	DurationTime = duration;
	LastUpdate = time;
	Texture = texture;
	Sprite = sprite;
	SpriteClip.X = 0;
	SpriteClip.Y = 0;
	SpriteClip.W = Texture->GetWidth();
	SpriteClip.H = Texture->GetHeight();
}

XParticle::~XParticle()
{
	Sprite = NULL;
	Texture = NULL;
}

void XParticle::SetCurrent(XColor color, XVector vec, int rotation, float scale)
{
	CurrentColor = color;
	CurrentTranslation = vec;
	CurrentRotation = rotation;
	CurrentScale = scale;
}

void XParticle::SetChange(XColor color, XVector vec, int rotation, float scale)
{
	ChangeColor = (color-CurrentColor)/(DurationTime/100);
	ChangeTranslation = (vec - CurrentTranslation)/(DurationTime/100);
	ChangeRotation = (rotation - CurrentRotation)/(DurationTime/100);
	ChangeScale = (scale - CurrentScale)/(DurationTime/100);
}

void XParticle::Update(int time)
{
	DurationTime -= time-LastUpdate;

	float delta = time - LastUpdate;

	
		CurrentColor += ChangeColor*(delta/100);
		CurrentTranslation += ChangeTranslation*(delta/100);
		CurrentRotation += ChangeRotation*(delta/100);
		CurrentScale += ChangeScale*(delta/100);
	

	LastUpdate = time;
}

void XParticle::Draw()
{
	if(DurationTime>0)
	{
		XPushTransformation();

		XSetColor(CurrentColor);
		XSetTranslation(CurrentTranslation);
		XSetScale(CurrentScale);
		XSetRotation(CurrentRotation);

		Sprite->Draw(XRectangle(0, 0, SpriteClip.W, SpriteClip.H), Texture, SpriteClip);

		XPopTransformation();
	}
}